package com.fenshi.interactor.imp;

import android.app.Activity;
import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.android.volley.Request;
import com.android.volley.VolleyError;
import com.fenshi.bean.RequestBean;
import com.fenshi.interactor.DamageOrderInteractor;
import com.fenshi.presenter.listener.RequestListener;
import com.fenshi.until.MethodCode;
import com.fenshi.until.MethodType;
import com.fenshi.until.PostUpLoadRequest;
import com.quickdv.helper.NetWorkImp;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lilongfei on 15/10/18.
 */
public class DamageOrderInteractorImp extends NetWorkImp implements DamageOrderInteractor {

    RequestListener listener;

    public DamageOrderInteractorImp(Context c, RequestListener listener) {
        super(c);
        this.listener = listener;
    }

    @Override
    public void updateFile(List<String> paths) {
        final Map<Integer, File> data = new HashMap<>();
        final JSONArray array = new JSONArray();
        int count = 0;
        for (String path : paths) {
            File file = new File(path);
            data.put(count, file);
            count++;
        }

        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {

                PostUpLoadRequest upfile = new PostUpLoadRequest();
                for (int i = 0; i < 4; i++) {
                    if (data.get(i) != null) {
                        String path = upfile.uploadFile(data.get(i),null);
                        try {
                            array.put(i, path);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }

                ((Activity) c).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        JSONObject obj = new JSONObject();
                        obj.put(MethodType.UPFILE, array);
                        listener.Success(MethodType.UPFILE, obj);
                    }
                });
            }
        });
        t.start();
    }

    @Override
    public void evidence(String orderid, String userId, String pic, String description) {
        RequestBean bean = new RequestBean();
        Map<String, Object> params = new HashMap<>();
        params.put("orderid", orderid);
        params.put("pic", pic);
        params.put("description", description);
        bean.setRequestParams(params);
        bean.setUserSession(userId);
        bean.setRequestCode(MethodType.EVIDENE);
        addFastJsonQueue(Request.Method.POST, MethodType.EVIDENE, (JSONObject) JSON.toJSON(bean));
        startRequest();
    }

    @Override
    protected void onRequestSuccess(Object tag, JSONObject result) {
        if (result == null) {
            onRequestExcption(null);
            return;
        }
        JSONObject response = result.getJSONObject(MethodCode.responseContent_key);
        if (response == null) {
            onRequestExcption(null);
        } else {
            int statue = response.getInteger(MethodCode.responseStatue);
            if (tag.equals(MethodType.EVIDENE)) {

                if (statue == MethodCode.NetState.SUCCESS.getValue()) {
                    listener.Success(tag.toString(), result);
                }

            }
        }
    }

    @Override
    protected void onRequestExcption(VolleyError error) {
        if (error == null) error = new VolleyError();
        listener.Exception("", error);
        log.e("DamageOrderInteractorImp", error.toString());
    }
}
